//小蛇
(function() {
    //小蛇构造函数
    var elements = []
    function Snake (width,height,direction) {
        this.width = width || 20
        this.height = height || 20
        //身体
        this.body = [
            {x:3,y:2,color:"red"},
            {x:2,y:2,color:"orange"},
            {x:1,y:2,color:"orange"}
        ]
        this.direction = direction || "right"
    }
    //小蛇原形方法
    Snake.prototype.init = function (map) {
        remove()
        for (var i = 0; i < this.body.length; i++) {
            var div = document.createElement("div")
            map.appendChild(div)
            div.style.width = this.width + "px"
            div.style.height = this.height + "px"
            div.style.backgroundColor = this.body[i].color
            div.style.position = "absolute"

            div.style.left = this.body[i].x * this.width +"px"
            div.style.top = this.body[i].y * this.width +"px"

            elements.push(div)
        }
    }
    //小蛇移动
    Snake.prototype.move = function (food,map) {
        for (var i = this.body.length - 1; i > 0; i--) {
            this.body[i].x = this.body[i-1].x
            this.body[i].y = this.body[i-1].y
        }
        switch (this.direction) {
            case "right" : this.body[0].x+=1 ;break;
            case "left" : this.body[0].x-=1 ;break;
            case "top" : this.body[0].y-=1 ;break;
            case "bottom" : this.body[0].y+=1 ;break;
        }

        //判断有没有迟到实物
        var headX = this.body[0].x * this.width
        var headY = this.body[0].y * this.height
        //是否碰到
        if(headX == food.x && headY == food.y) {
            var last = this.body[this.body.length-1]
            
            this.body.push({
                x:last.x,
                y:last.y,
                color:last.color
            })
            food.init(map)
        }
    }
    //删除小蛇数据
    function remove() {
        var i = elements.length-1
        for (;i >= 0 ; i--) {
            var ele = elements[i]
            ele.parentNode.removeChild(ele)
            elements.splice(i,1)
        }
    }
    window.Snake = Snake
}());